package cn.jianyongjie.basic.controller;

import cn.jianyongjie.basic.dto.LoginDto;
import cn.jianyongjie.basic.dto.VerificationCode;
import cn.jianyongjie.basic.dto.WechatLogin;
import cn.jianyongjie.basic.exception.CustomException;
import cn.jianyongjie.basic.service.ILoginService;
import cn.jianyongjie.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
* @Description: 登录
* @Author: JianYongJie
* @Version: 1.0
* @Date:  2022/5/10 10:57
*/
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;


    /*
    * @Title: LoginController
    * @Description: 登录
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/10 16:34
    * @Parameters: * @param dto
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PostMapping("/administrators")
    public AjaxResult administrators(@RequestBody LoginDto dto){
        try {
            HashMap<String, Object> map = loginService.administrators(dto);
            // 返回Ajax
            return AjaxResult.success().setResultObj(map);

        }catch (CustomException e){
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        }

    }


    /*
    * @Title: LoginController
    * @Description: 微信扫码登录
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/12 14:59
    * @Parameters: * @param code
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatLogin code){
        try {
            // 返回Ajax
            return loginService.wechat(code);

        }catch (CustomException e){
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        }

    }

    /*
    * @Title: LoginController
    * @Description: 授权绑定注册
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/12 21:16
    * @Parameters: * @param code
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatLogin code){
        try {

            HashMap<String, Object> map = loginService.binder(code);
            // 返回Ajax
            return AjaxResult.success().setResultObj(map);

        }catch (CustomException e){
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        }

    }

}
